TestCase("ListControllerInitTest", {
	setUp: function () {
		this.view = document.createElement("ol");
		this.backend = new tddjs.TodoList();
		this.controller = new tddjs.ListController(this.view, this.backend);

		var items =[];

		sinon.stub(this.backend, "all").yields( null,items );

		this.addDone = function (text) {
			this.add(text,true);
		};
		this.addTodo = function (text) {
			this.add(text,false);
		};
		this.add = function(text,done) {
			items.push({
				text:text,
				done:done,
				id:items.length
			});
		}
	},
	"test should display items from this.backend in list": function () {
		this.addTodo("Brush teeth");
		this.addTodo("Take shower");
		this.addTodo("Learn TDD");

		this.controller.init();

		var lis = this.view.childNodes;
		assertEquals(3, lis.length);
		assertEquals("Brush teeth", lis[0].innerHTML);
		assertEquals("Take shower", lis[1].innerHTML);
		assertEquals("Learn TDD", lis[2].innerHTML);
	},
	"test should display todo items with class todo": function () {
		this.addTodo("Brush teeth");

		this.controller.init();

		assertClassName("todo", this.view.firstChild);
	},
	"test should display done items with class done": function () {
		this.addDone("Brush teeth");

		this.controller.init();

		assertClassName("done", this.view.firstChild);
	},
	"test should sort todo items to the top": function () {
		this.addDone("Brush");
		this.addTodo("Shower");
		this.addDone("TDD");

		this.controller.init();

		var lis = this.view.childNodes;
		assertEquals("Shower", lis[0].innerHTML);
		assertEquals("Brush", lis[1].innerHTML);
		assertEquals("TDD", lis[2].innerHTML);
	},
	"test should display error message if server fails": function () {
		this.backend.all = function (callback) {
			callback({
				message: "Server do bad"
			});
		};
		this.controller.init();

		assertClassName("error", this.view.firstChild);
		assertEquals("Server failed to get todo items",
		this.view.firstChild.innerHTML);
	},
	"test should mark as done when clicked": function () {
		this.backend = sinon.mock(this.backend);
		this.backend.expects("completeItem").yields(null,null).once();
		this.addTodo("TDD");
		this.controller.init();

		jQuery(this.view.firstChild).trigger("click");

		this.backend.verify();
		assertClassName("done", this.view.firstChild);
	},
	"test should show error message if server fails": function () {
		this.backend.completeItem = function (id, callback) {
			callback({
				message: "bad server"
			}, null);
		};
		this.addTodo("TDD");
		this.controller.init();
		
		jQuery(this.view.firstChild).trigger("click");

		assertClassName("error", this.view.firstChild);
		assertEquals("bad server", this.view.firstChild.innerHTML);
	},

});